<?php
class peopleModel {

	var $DB;

	function peopleModel ( $DB ){
		$this->DB = $DB;
	}

	function isLoginned() {
		if(!empty($_SESSION['perisLogin']))
			return true;
		else
			return false;
	}

	function getUserbasic($uid){
		$sql = "SELECT * FROM member WHERE uid = '$uid'";
		$this->DB->query($sql);
		return $this->DB->fetchRow();
	}

	function getComments($uid,$type,$order,$number){
		$sql = "SELECT c.mid,m.title,m.thumb FROM movie_comments AS c,movie AS m WHERE c.uid='$uid' AND c.mid=m.id AND c.type='$type' ORDER BY $order DESC LIMIT 0,$number";
		$this->DB->query($sql);
		return $this->DB->fetchAll();
	}

	function getReviews($uid,$order,$number){
		$sql = "SELECT r.id,r.uid,r.mid,r.title,r.contents,r.reply,m.username,m.avatars,m2.title mtitle,m2.thumb
		FROM movie_review AS r,member AS m,movie AS m2 
		WHERE r.uid = '$uid' AND r.uid = m.uid AND r.mid = m2.id ORDER BY $order DESC LIMIT 0,$number ";
		$this->DB->query($sql);
		return $this->DB->fetchAll();	
	}

	function getFriends($uid,$order,$number){
		$sql = "SELECT f.uid as fuid,m.username,m.avatars FROM member_friend AS f,member AS m WHERE f.uid='$uid' AND f.fid = m.uid ORDER BY $order DESC LIMIT 0,$number ";
		$this->DB->query($sql);
		return $this->DB->fetchAll();	
	}
	
	function isContact($myuid,$uid){
		$sql = "SELECT * FROM member_friend WHERE uid = '$myuid' AND fid = '$uid' ";
		$this->DB->query($sql);
		return $this->DB->fetchRow();
	}

	function addContact($myuid,$uid){
		$sql = "INSERT INTO member_friend (uid,fid) VALUES ('$myuid','$uid')";
		$this->DB->query($sql);	
		return $this->DB->insertID();
	}

	function cancelContact($myuid,$uid){
		$sql = "DELETE FROM member_friend WHERE uid = '$myuid' AND fid = '$uid' " ;
		$this->DB->query($sql);	
		return $this->DB->affectedRows();
	}


}
?>
